package com.qdsg.modulesihealsdk.detectStrategy.control;

import com.qdsg.modulesihealsdk.R;
import com.qdsg.modulesihealsdk.utils.SihealSdkConstant;
import com.qdsg.modulesihealsdk.detectStrategy.strategy.bloodFatStrategy.BloodFatStrategy;
import com.qdsg.modulesihealsdk.detectStrategy.strategy.Spo2Strategy.Spo2Strategy;
import com.qdsg.modulesihealsdk.detectStrategy.strategy.bpStrategy.BpStrategy;
import com.qdsg.modulesihealsdk.detectStrategy.strategy.uaStrategy.UaStrategy;
import com.qdsg.modulesihealsdk.detectStrategy.strategy.bfStrategy.BfStrategy;
import com.qdsg.modulesihealsdk.detectStrategy.strategy.hwbmiStrategy.HwBmiStrategy;
import com.qdsg.modulesihealsdk.detectStrategy.strategy.hbStrategy.HbStrategy;
import com.qdsg.modulesihealsdk.detectStrategy.strategy.tStrategy.TStrategy;
import com.qdsg.modulesihealsdk.detectStrategy.strategy.gluStrategy.GluStrategy;

import java.util.HashMap;

/**
 * @Description
 * @Author WuJianFeng
 * @Date 2022/11/21 14:49
 */
public class DetectFactory {

    public static volatile DetectFactory detectFactory;
    public static DetectFactory getInstance(){
        if (detectFactory == null){
            synchronized (DetectFactory.class){
                if (detectFactory == null){
                    detectFactory = new DetectFactory();
                }
            }
        }
        return detectFactory;
    }

    private DetectFactory(){
        initMap();
    }

    private final HashMap<Byte,DetectBusiness> detectMap = new HashMap<Byte,DetectBusiness>();

    private void initMap() {
        detectMap.put(SihealSdkConstant.channelID_bloodFat, new BloodFatStrategy());
        detectMap.put(SihealSdkConstant.channelID_SPO2, new Spo2Strategy());
        detectMap.put(SihealSdkConstant.channelID_BP, new BpStrategy());
        detectMap.put(SihealSdkConstant.channelID_UA, new UaStrategy());
        detectMap.put(SihealSdkConstant.channelID_BF, new BfStrategy());
        detectMap.put(SihealSdkConstant.channelID_HWBMI, new HwBmiStrategy());
        detectMap.put(SihealSdkConstant.channelID_HB, new HbStrategy());
        detectMap.put(SihealSdkConstant.channelID_T,new TStrategy());
        detectMap.put(SihealSdkConstant.channelID_glu,new GluStrategy());
    }

    //释放资源
    public void clear(){
        detectMap.clear();
        detectFactory = null;
    }

    public void stopThread(){
        DetectDeal.getInstance().stopThread();
    }

    public DetectBusiness getDetectStrategy(Byte channelId) {
        if (channelId == SihealSdkConstant.channelID_invalid){
            throw new IllegalArgumentException(SihealSdkConstant.SihealSdkAppContext.getResources().getString(R.string.channelID_invalid));
        }
        if (null == detectMap.get(channelId)){
            throw new NullPointerException(SihealSdkConstant.SihealSdkAppContext.getResources().getString(R.string.strategy_empty));
        }


        if (detectMap.get(channelId) != null){
            DetectDeal.getInstance().setDetectStrategy(detectMap.get(channelId));
        }

        // wjf 务必保证 detectMap[channelID]不为空
        return detectMap.get(channelId);
    }
}
